package com.jshoperxms.service.impl;

import com.jshoperxms.controller.utils.BaseTools;
import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.statickey.SerialKeys;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.dao.SysUserRoleRpTDao;
import com.jshoperxms.entity.SysUserRoleRpT;
import com.jshoperxms.service.SysUserRoleRpTService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by sdywcd on 16/11/1.
 * Des:
 */
@Service("sysUserRoleRpTService")
@Scope("prototype")
public class SysUserRoleRpTServiceImpl extends BaseTServiceImpl<SysUserRoleRpT> implements SysUserRoleRpTService {


    @Resource
    private Serial serial;

    @Resource
    private SysUserRoleRpTDao sysUserRoleRpTDao;

    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public void updateSysUserRoleTransactional(String basicuserid, String sysroleids) {

        //1,删除用户所有角色
        Criterion criterion=Restrictions.eq("basicuserid",basicuserid);
        List<SysUserRoleRpT> list=this.sysUserRoleRpTDao.findByCriteria(SysUserRoleRpT.class,criterion);
        if(!list.isEmpty()){
            this.sysUserRoleRpTDao.deleteAll(list);
        }
        //2,添加用户角色
        String ids[]= StringUtils.split(sysroleids, TagWords.SPLITDOT);
        for(String s:ids){
            SysUserRoleRpT bean=new SysUserRoleRpT();
            bean.setId(this.serial.Serialid(SerialKeys.SYSUSERROLERPT));
            bean.setBasicuserid(basicuserid);
            bean.setCreatorid(BaseTools.getAdminCreateId());
            bean.setCreatetime(DateTools.getSystemTimeBi());
            bean.setStatus(BaseEnums.DataUsingState.USING.getState());
            bean.setSysroleid(s);
            bean.setUpdatetime(DateTools.getSystemTimeBi());
            bean.setVersiont(1);
            this.sysUserRoleRpTDao.save(bean);
        }

    }
}
